home *** CD-ROM | disk | FTP | other *** search
- Path: brighton.openmarket.com!decwrl!sony!sonysjc!usenet
- From: Michael Rizzo <rizzom@mars.superlink.net>
- Newsgroups: comp.lang.c++
- Subject: Saving the contents of an object to a file
- Date: Tue, 16 Apr 1996 09:15:47 -0400
- Organization: Sony Corporation of America, San Jose, CA
- Message-ID: <31739D83.5F61@mars.superlink.net>
- NNTP-Posting-Host: rizzom.rmpg.sel.sony.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (Win95; I)
-
- Hi,
-
- I am having a problem saving the contents of an object to a
- file. For instance, I have the following code:
-
- class A
- {
- private:
- int id;
- char *name;
- public:
- A(int, char*);
- A();
- ~A();
- char * GetName() {return name;}
- int GetId() {return id;}
-
- };
-
- A::A(int i, char *n)
- {
- int temp;
- id = i;
- temp = strlen(n);
- name = new char[temp+1];
- strcpy(name,n);
- }
-
- A::A()
- {
- id = 0;
- name = new char[5];
- strcpy(name, "none");
- }
-
- int main()
- {
- A x(10,"JOE"), y;
- ofstream outf("savefile",ios::binary);
- outf.write((char*) &x, sizeof x);
- outf.write((char*) &y, sizeof y);
- outf.close();
- return 0;
- }
-
- This produces a file "savefile" with size 0. What am I doing wrong?
- BTW, I am using DJGPP v2 to compile this.
-
- Any help would be greatly appreciated.
-
- Mike
- rizzom@mars.superlink.net
-